//-------------------------------------------------
//		youtube playlist jquery plugin
//		Created by dan@geckonm.com
//		www.geckonewmedia.com
//
//		v1.8 - updated to allow html5
//           - modified by Ceasar Feijen www.cfconsultancy.nl
//-------------------------------------------------

jQuery.fn.ytplaylist = function (options) {
    var options = jQuery.extend({
        holderId: 'ytvideo',
        playerHeight: '385',
        playerWidth: '640',
        addThumbs: true,
        thumbSize: 'small',
        autoPlay: true,
        playOnLoad: false,
        playfirst: 0,
        start: 0,
        showRelated: false,
        showInfo: true,
        wmode: true,
        iv_load_policy: true,
        hd: false,
        autoHide: true,
        modestbranding: true,
        html5: true,
        playerColor: 'ffffff',
        playerversion: '&version=2',
        probably_logged_in: true,
        allowFullScreen: true
    }, options);
    var selector = jQuery(this);
    var autoPlay = "";
    var showRelated = "&rel=0";
    var showInfo = "&showinfo=0";
    var wmode = "t";
    var autoHide = "";
    var modestbranding = "";
    var fullScreen = "";
    var iv_load_policy = "";
    var hd = "";
    var probably_logged_in = "&probably_logged_in=0";
    var playerversion = "";
    if (options.showRelated) showRelated = "&rel=1";
    if (options.showInfo) showInfo = "&showinfo=1";
    if (options.wmode) wmode = "&wmode=opaque";
    if (options.autoHide) autoHide = "&autohide=1";
    if (options.allowFullScreen) fullScreen = "&fs=1";
    if (options.iv_load_policy) iv_load_policy = "&iv_load_policy=3";
    if (options.modestbranding) modestbranding = "&modestbranding=1";
    if (options.probably_logged_in) probably_logged_in = "&probably_logged_in=1";
    if (options.hd) hd = "&hd=1";

    function play(id) {
        if (options.autoPlay && options.playOnLoad) autoPlay = "&autoplay=1";
        options.playOnLoad = true;

        var html = '';
        if (id == false) {
        	return html
        }
        else {

        if (options.html5) {
		html += '<iframe class="youtube-player" type="text/html" width="' + options.playerWidth + '" height="' + (options.playerHeight-3) + '" src="http://www.youtube.com/embed/' + id + '?' + autoPlay + autoHide + showRelated + showInfo + wmode + probably_logged_in + modestbranding + '" frameborder="0">';
		html += '</iframe>';
		}
		else {

        html += '<object height="' + options.playerHeight + '" width="' + options.playerWidth + '">';
        html += '<param name="movie" value="http://www.youtube.com/v/' + id + options.playerversion + autoPlay + showRelated + showInfo + fullScreen + iv_load_policy + hd + '&color1=0x' + options.playerColor + '&color2=0x' + options.playerColor + autoHide + modestbranding + '" />';
        html += '<param name="wmode" value="transparent" />';
        html += '<param name="allowscriptaccess" value="always" />';
        html += '<param name="bgcolor" value="#000000" />';
        if (options.allowFullScreen) {
            html += '<param name="allowfullscreen" value="true" /> '
        }
        html += '<embed src="http://www.youtube.com/v/' + id + options.playerversion + autoPlay + showRelated + showInfo + fullScreen + iv_load_policy + hd + '&color1=0x' + options.playerColor + '&color2=0x' + options.playerColor + autoHide + modestbranding + '"';
        if (options.allowFullScreen) {
            html += ' allowfullscreen="true" '
        }
        html += 'type="application/x-shockwave-flash" wmode="transparent" bgcolor="#000000" allowscriptaccess="always" height="' + options.playerHeight + '" width="' + options.playerWidth + '"></embed>';
        html += '</object>';
        }
        return html
        }
    };

    function youtubeid(url) {
        if (url == undefined) {
            return false
        } else {
            var ytid = url.match("[\\?&]v=([^&#]*)");
            ytid = ytid[1];
            return ytid
        }
    };
    var firstVid = jQuery(selector).filter(function (index) {
        return index == options.playfirst
    }).addClass('currentvideo').attr('href');
    jQuery('#' + options.holderId + '').html(play(youtubeid(firstVid)));
    selector.click(function () {
        jQuery('#' + options.holderId + '').html(play(youtubeid(jQuery(this).attr('href'))));
        jQuery(selector).filter('.currentvideo').removeClass('currentvideo');
        jQuery(this).addClass('currentvideo');
        return false
    });
    if (options.addThumbs) {
        selector.each(function (i) {
            var replacedText = jQuery(this).text();
            var replacedTextTitle = jQuery(this).text().substring(0, 35);
            if (options.thumbSize == 'small') {
                var thumbUrl = 'http://img.youtube.com/vi/' + youtubeid(jQuery(this).attr('href')) + '/2.jpg'
            } else {
                var thumbUrl = 'http://img.youtube.com/vi/' + youtubeid(jQuery(this).attr('href')) + '/0.jpg'
            }
            jQuery(this).empty().html('<img src="' + thumbUrl + '" alt="' + replacedTextTitle + '" />' + replacedText).attr('title', replacedTextTitle)
        })
    }
};